package extrace.loader;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import java.util.List;

import extrace.misc.model.ExpressSheet;
import extrace.misc.model.PackageRoute;
import extrace.misc.model.TransHistory;
import extrace.misc.model.TransPackage;
import extrace.net.HttpAsyncTask;
import extrace.net.HttpResponseParam.RETURN_STATUS;
import extrace.net.IDataAdapter;
import extrace.net.JsonUtils;
import extrace.ui.domain.ExpressEditActivity;
import extrace.ui.main.ExTraceApplication;


public class HistoryLoader extends HttpAsyncTask {

    String url;

    private Activity context;
    IDataAdapter<List<PackageRoute>> adapter;
    public HistoryLoader(IDataAdapter<List<PackageRoute>> adpt,Activity context) {
        super(context);
        this.context = context;
        adapter = adpt;
        url = ((ExTraceApplication) context.getApplication()).getDomainServiceUrl();
    }

    @Override
    public void onDataReceive(String class_name, String json_data) {
        if (json_data.equals("上传成功")) {

//            Toast.makeText(context, json_data + class_name, Toast.LENGTH_SHORT).show();
        } else if (json_data.equals("[]")) {

//          Toast.makeText(context,"等待揽收 (无路线数据)", Toast.LENGTH_SHORT).show();

        } else {
            List<PackageRoute> packageRoutes = JsonUtils.fromJson(json_data, new TypeToken<List<PackageRoute>>() {
            }, "yyyy-MM-dd'T'HH:mm:ss+hh:");

          adapter.setData(packageRoutes);
          adapter.notifyDataSetChanged();


        }
//        else if(class_name.equals("E_ExpressSheet"))		//已经存在
//        {
//            Toast.makeText(context, json_data, Toast.LENGTH_SHORT).show();
////			ExpressSheet ci = JsonUtils.fromJson(json_data, new TypeToken<ExpressSheet>(){});
////			adapter.setData(ci);
////			adapter.notifyDataSetChanged();
////			Toast.makeText(context, "快件运单信息已经存在!", Toast.LENGTH_SHORT).show();
//        }
//        else if(class_name.equals("R_ExpressSheet"))		//保存完成
//        {
//            ExpressSheet ci = JsonUtils.fromJson(json_data, new TypeToken<ExpressSheet>(){});
//            adapter.getData().setID(ci.getID());
//            adapter.getData().onSave();
//            adapter.notifyDataSetChanged();
//            Toast.makeText(context, "快件运单信息保存完成!", Toast.LENGTH_SHORT).show();
//        }
//        else
//        {
//        }
    }

    @Override
    public void onStatusNotify(RETURN_STATUS status, String str_response) {
        // TODO Auto-generated method stub

    }

    public void sendposition(String id, float x, float y) {
        String pid = ((ExTraceApplication) context.getApplication()).getLoginUser().geTransPackageID();
        url += "saveLocation/" + id + "/" + x + "/" + y + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getPackageroute(String id) {
        url += "getPackageroute/" + id + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void New(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "newExpressSheet/id/" + id + "/uid/" + uid + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Save(ExpressSheet es) {
        String jsonObj = JsonUtils.toJson(es, true);
        url += "saveExpressSheet";
        Log.d("23", url);
        try {
            execute(url, "POST", jsonObj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Receive(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "receiveExpressSheetId/id/" + id + "/uid/" + uid + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Dabao(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "DabaoExpressSheetId/id/" + id + "/uid/" + uid + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Chaibao(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "ChaibaoExpressSheetId/id/" + id + "/uid/" + uid + "?_type=json";
        Log.d("23", url);
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dispatch(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "dispatchExpressSheetId/id/" + id + "/uid/" + uid + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Delivery(String id) {
        int uid = ((ExTraceApplication) context.getApplication()).getLoginUser().getUID();
        url += "deliveryExpressSheetId/id/" + id + "/uid/" + uid + "?_type=json";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
